	var tab_counter = 0;
	var id_pi;
	var id_pi_to=0;
	var name_pi_to=null;
	var c_msg=null;

	$(function() {

		// tabs init with a custom tab template and an "add" callback filling in the content
		$("#tabs")
				.tabs(
						{
							tabTemplate : '<li><a href="#{href}">#{label}</a><span class="ui-icon ui-icon-close">Remove Tab</span></li>',
							add : function(event, ui) {
								var tab_content = '<textarea name="'+name_pi_to+'" id="'+name_pi_to+'" class="txt_area" cols="100" rows="15" dir="ltr" readonly>Chat With '
										+ name_pi_to
										+ '</textarea><input type="hidden" value="'+id_pi_to+'">';
								$(ui.panel).append(tab_content);
							}
						});

		$("#tabs" ).tabs({
			   select: function(event, ui) {
				   name_pi_to=ui.panel.getElementsByTagName("textarea")[0].name;
				   id_pi_to=ui.panel.getElementsByTagName("input")[0].value;
			   }
			});
		// close icon: removing the tab on click
		$("#tabs span.ui-icon-close").live("click", function() {
			if (window.confirm("Are you sure of terminating this session?")) {
				var index = $("li", "#tabs").index($(this).parent()); //get the index of the tab the event is triggered
				$("#tabs").tabs("remove", index);
				//tab_counter--;
			} else {
				return;
			}
		});
		
		chat_init();
	});
	
	function onclick_friend_list(name, id_pi) {
		if (confirm("Start the Session with \"" + name + "\" ?")) {
			alert("Start a new Session with \"" + name + "\"");
			tab_counter++;
			name_pi_to=name;
			id_pi_to=id_pi;
			$("#tabs").tabs("add", "#tabs-" + name, name);
		} else {
			return;
		}
	}

	function onclick_insSending(){
		var msg=document.getElementById("insConSending").value;
	
		if(msg==null||msg==""){
			alert("Please input the message");
		}else{
			if(id_pi_to==null||id_pi_to==0){
				alert("Please choose the person you want to talk");
			}else{
				id_pi=document.getElementById("id_pi").value
				//alert(id_pi);
				sendToServer(id_pi,id_pi_to,msg);
				//alert("s");
			}
		}
		//showInformation(strTextSending);
	}
	
	 function sendToServer(from,to,msg){
		 //alert("STS");
		 c_msg=msg;
		var MG={'pFrom':from,'pTo':to,'msg':msg};
		//alert(MG);
		$.ajax({
			type: "POST",
	        url: "../StoreIMIntoMsg",
	        data: MG,
	        success: callbackMG,
	        dataType: "json"
	        });
	}
	
	function callbackMG(data){
		if(data.result){
			$("#"+name_pi_to).innerHTML="ME @ "+new Date()+"\n\t"+c_msg;
			document.getElementById(name_pi_to).value+="\n\n"+"ME @ "+new Date()+"\n\t"+c_msg;
		}else{
			alert("Error");
		}
	}
	
	function showInformation(from_name,msg,time,id_user_from){
		var info=from_name+" @ "+time+"\n\t"+msg;
		alert("New Information from "+info);
		if($("#"+from_name)!=null){
			//alert("show");
			//alert(document.getElementById(from_name).value);
			document.getElementById(from_name).value+="\n\n"+info;
		}else{
			onclick_friend_list(from_name, id_user_from);
			showInformation(from_name,msg,time,id_user_from);
		}
	}